import akka.actor.{ActorSystem, PoisonPill, Props}
import akka.cluster.client.ClusterClientReceptionist
import akka.cluster.singleton.{ClusterSingletonManager, ClusterSingletonManagerSettings, ClusterSingletonProxy, ClusterSingletonProxySettings}
import akka.event.Logging
import com.typesafe.config.ConfigFactory

/**
  * Created by dl 
  */
object ClusterNodeMain {
  def main(args: Array[String]): Unit = {
    val conf=ConfigFactory.load("node")
    val system=ActorSystem("mycluster", conf)


    val log = Logging.getLogger(system, this)

    val manager = system.actorOf(
      ClusterSingletonManager.props(Props[Master],
        PoisonPill,
        ClusterSingletonManagerSettings(system).withRole("compute")),
      "masterManager")  //单例管理器的路径为/user/masterManager。系统会创建相应actor单例，其路径为/user/masterManager/singleton

    log.info(s"created singleton manager, at path: {}", manager.path)

    val proxy = system.actorOf(ClusterSingletonProxy.props(singletonManagerPath = "/user/masterManager",
      settings = ClusterSingletonProxySettings(system).withRole("compute")),
      name = "masterProxy")  //单例代理类，准备让单例接收的消息都应发到代理


    ClusterClientReceptionist(system).registerService(proxy)

    log.info("created singleton proxy, at path: {}", proxy.path)
  }

}
